#include "StdAfx.h"
#include "EntityManager.h"
#include "BaseEntity.h"

EntityManager::EntityManager(void)
{
}

EntityManager::~EntityManager(void)
{
}


/** Update each entity */
void EntityManager::Update( float dt )
{
    Prune( m_entities );
    type_EntList::iterator it = m_entities.begin();
    for( ; it != m_entities.end(); ++it  )
    {
        BaseEntity* ent = (*it);
        if ( ent->IsActive() )
        {
            ent->Update( dt );
        }
    }
}

/** Add an Entity to the world */
void EntityManager::Add( BaseEntity* ent )
{
    m_entities.push_back( ent );
}


/** Remove Entity from the world */
void EntityManager::Remove( BaseEntity* entToRemove )
{
    type_EntList::iterator it = m_entities.begin();
    for( ; it != m_entities.end(); ++it )
    {
        BaseEntity* ent = (*it);
        if ( ent == entToRemove )
        {
            m_entities.erase( it );
            delete ent;

            return;
        }
    }
}

/** Remove Entity from the world */
void EntityManager::Remove( type_id id )
{
    type_EntList::iterator it = m_entities.begin();
    for( ; it != m_entities.end(); ++it )
    {
        BaseEntity* ent = (*it);
        if ( ent->GetID() == id )
        {
            m_entities.erase( it );
            delete ent;

            return;
        }
    }
}

/** Remove Entity from the world */
void EntityManager::Remove( const std::string &name )
{
    type_EntList::iterator it = m_entities.begin();
    for( ; it != m_entities.end(); ++it )
    {
        BaseEntity* ent = (*it);
        if ( ent->GetName() == name )
        {
            m_entities.erase( it );
            delete ent;

            return;
        }
    }
}

/** Clear all Entities */
void EntityManager::Clear()
{
    type_EntList::iterator it = m_entities.begin();
    for( ; it != m_entities.end(); ++it )
    {
        BaseEntity* ent = (*it);
        delete ent;
    }
    m_entities.clear();
}


/** Get an Entity */
BaseEntity* EntityManager::Get( type_id id )
{
    type_EntList::iterator it = m_entities.begin();
    for( ; it != m_entities.end(); ++it )
    {
        BaseEntity* ent = (*it);
        if ( ent->GetID() == id )
        {
            return ent;
        }
    }
    return NULL;
}

/** Get an Entity */
BaseEntity* EntityManager::Get( const std::string &name )
{
    type_EntList::iterator it = m_entities.begin();
    for( ; it != m_entities.end(); ++it )
    {
        BaseEntity* ent = (*it);
        if ( ent->GetName() == name )
        {
            return ent;
        }
    }
    return NULL;
}

/*---------------------------------------------------------------------------------
    Private Functions
----------------------------------------------------------------------------------*/

/** Prune for entities that need to be removed */
void EntityManager::Prune(type_EntList &list)
{
    type_EntList::iterator it = list.begin();
    for( ; it != list.end();  )
    {
        BaseEntity* ent = (*it);
        if ( ent->CanRemove() )
        {
            it = list.erase( it );
            delete ent;
        }
        else ++it;
    }

}